﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;

namespace Tayninh2011
{
    public sealed partial class FrmMain : Form
    {
        private string _strConnectionStatus="Chưa kết nối CSDL";
        private string _strDatabase = "";
        private IWorkspaceEdit _sdeWorkspace;
        private IWorkspace _mdbWorkspace;

        
        private string _sdeEnvironment="";
        private string _strPathTempFolder = "C:\\tayninh\\temp";
        private string _strNameTempMdbFile = "tempmdb";
        private static bool isShown = false;
        private static readonly FrmMain myform = new FrmMain();
        private IToolbarMenu menu = new ToolbarMenuClass();
        private FrmMap frmMap = FrmMap.FormMap;
        private string[,] _arrUserInfo;
        private SqlConnection _sqlConnection = null;

        

        public IWorkspace MdbWorkspace
        {
            get { return _mdbWorkspace; }
        }

        public SqlConnection SqlConnection
        {
            get { return _sqlConnection; }
        }

        public void SetUserInfo(string[,] info)
        {
            this._arrUserInfo = info;
        }
        public string SdeEnvironment
        {
            get { return _sdeEnvironment; }
            set { _sdeEnvironment = value; }
        }

        public IWorkspaceEdit SdeWorkspace
        {
            get { return _sdeWorkspace; }
            set { _sdeWorkspace = value; }
        }
        //public string strDatabase = "aa";

        public string StrDatabase
        {
            get { return _strDatabase; }
            set { _strDatabase = value; }
        }
        
        public string StrConnectionStatus
        {
            get { return _strConnectionStatus; }
            set { _strConnectionStatus = value; }
        }
        public static FrmMain frmMain
        {
            get { return myform; }
        }
        private FrmMain()
        {
            InitializeComponent();
            
            staConnect.Text = this.StrConnectionStatus;
            
            ITnToolsWorkspace createTempWsp = new TnTools();    
            _mdbWorkspace=createTempWsp.CreateWorkspaceGdbFile(_strPathTempFolder, _strNameTempMdbFile, "mdb");

            
            
        }
        public static FrmMain FormMain
        {
            get { return myform; }
        }
        public new void Show()
        {
            if (isShown)
            {
                base.Show();
            }
            else
            {
                base.Show();
                isShown = true;
            }
        }
        
        //static FrmMain()
        //{
        //    FormMain.FormClosing += new FormClosingEventHandler(formMain_FormClosing);
        //}
        //private static void formMain_FormClosing(object sender, FormClosingEventArgs e)
        //{
        //    e.Cancel = true;
        //    isShown = false;
        //    FormMain.Hide();
        //    Application.Exit();
        //}
        //dam bao ko tao moi form moi khi goi
        //#region Singleton
        //static FrmMain me = null;
        //static public FrmMain GetObj()
        //{
        //    if (me != null)
        //    {
        //        me=new FrmMain();
        //    }
        //    return me;
        //}
        //#endregion

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void submnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn thoát khỏi chương trình ?", "Thoát", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void subMnDangnhap_Click(object sender, EventArgs e)
        {

        }

        private void subMnTuychinhKetnoi_Click(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                //foreach (Form form in this.MdiChildren)
                //{
                //    if (form.GetType() == typeof(FrmConnection))
                //    {
                //        throw new Exception();
                //    }
                //}
                //FrmConnection frmNew = new FrmConnection();
                //frmNew.MdiParent = this;
                //frmNew.Show();
                FrmLogin frmLogin = FrmLogin.CallMe;
                
                frmLogin.Show();
            }
            catch (Exception err) { }
            //catch(Exception err){}
        }
        public void SetConnectionStatus(string text,bool status)
        {
            //MessageBox.Show(connectionStatus);
            staConnect.Text = text;
            StrConnectionStatus=text;
            if (status==true)
            {
                staConnect.ForeColor = Color.Green;
                subMenuTinh.Enabled = true;
            }
            else
            {
                staConnect.ForeColor = Color.Red;
                subMenuTinh.Enabled = false;
                
            }
        }
        public void SetDatabase(string database)
        {
            staDatabase.Text = database;
            StrDatabase = database;
        }
        public void SetWorkspace(IWorkspaceEdit workspace)
        {
            SdeWorkspace = workspace;
        }

        private void subMenuTinh_Click(object sender, EventArgs e)
        {
            try
            {
                //foreach (Form form in this.MdiChildren)
                //{
                //    if (form.GetType() == typeof(FrmCalculateLandPrize))
                //    {
                //        throw new Exception();
                //    }
                //}
                FrmCalculateLandPrize frmLandPrize = FrmCalculateLandPrize.FormCalculateLandPrize;
                frmLandPrize.MdiParent = this;
                frmLandPrize.StrDatabase = this.StrDatabase;
                
                frmLandPrize.Show();
                //frmLandPrize.SetDatabase(this.StrDatabase);
                frmLandPrize.SetWorkspace(this.SdeWorkspace);
                frmLandPrize.SdeEnvironment = this.SdeEnvironment;
                frmLandPrize.StrPathTempFolder = this._strPathTempFolder;
                frmLandPrize.StrNameTempMdbFile = this._strNameTempMdbFile;
                frmLandPrize.SetUserInfo(_arrUserInfo);
                frmLandPrize.SqlConnection = this._sqlConnection;
                //MessageBox.Show(frmLandPrize.Workspace.ToString());
                
            }
            catch (Exception err) { MessageBox.Show(err.ToString()); }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //FrmConnection frmConnect = new FrmConnection();
            FrmLogin frmLogin = FrmLogin.CallMe;
            frmLogin.Show();
            this.Enabled = false;
            //this.TopLevel = false;
            frmLogin.TopLevel = true;
            
            //frmConnect.MdiParent = this;
            //frmConnect.Show();
            
            //frmConnect.TopLevel = true;
            //this.Enabled = false;
            //this.
            //frmConnect.Enabled = true;

            
        }

        private void closeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //MessageBox.Show(SdeWorkspace.GetType() + " " + SdeWorkspace.IsBeingEdited().ToString());
            if (SdeWorkspace!=null)
            {
                SdeWorkspace.StopEditOperation();
                SdeWorkspace.StopEditing(true);
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(
            //ITnToolsWorkspace workspaceTool = new TnTools();
            //IFeatureClass fc = workspaceTool.OpenFeatureClassFromSDE(this.SdeWorkspace,"tn_duong");
            //IFeatureLayer fl = new FeatureLayerClass();
            //fl.FeatureClass=fc;
            //ILayer layer=(ILayer)fl;
            //layer.Name = fc.AliasName;
            //axMapControl1.AddLayer(layer);
            //axMapControl1.Map.Name = "Các lớp bản đồ";
            //axMapControl1.ActiveView.Refresh();
            //axTOCControl1.SetBuddyControl(axMapControl1);
            //axTOCControl1.Update();
            //axTOCControl1.Refresh();
            //frmMap.SetSdeWorkspace(this.SdeWorkspace);
            //frmMap.MdiParent = this;
            //frmMap.Show();
        }

        private void axLicenseControl1_Enter(object sender, EventArgs e)
        {

        }

        
        private void subMenu()
        {
            MessageBox.Show("hihi");
        }

        private void danhMụcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //FrmDanhmuc danhmuc = new FrmDanhmuc(this._arrUserInfo);
            ////danhmuc.SetUserInfo(this.arrUserInfo);
            //danhmuc.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.SdeEnvironment = this._sdeEnvironment;
            frm2.SdeWorkspaceEdit = this._sdeWorkspace;
            frm2.Show();
            
        }

        private void FrmMain_EnabledChanged(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                ITnToolsTable toolTable = new TnTools();
                this._sqlConnection = toolTable.TnOpenConnection(this._arrUserInfo);
            }
        }

        private void itemMenuDonvihanhchinh_Click(object sender, EventArgs e)
        {
            FrmDanhmuc danhmuc = new FrmDanhmuc(this._arrUserInfo);
            //danhmuc.SetUserInfo(this.arrUserInfo);
            danhmuc.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            //calcTool= calcFactory.GetCalculator(EnumModeCalc.mode3010);
            //MessageBox.Show(calcTool.Name);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            //calcTool = calcFactory.GetCalculator(EnumModeCalc.mode3020);
            //MessageBox.Show(calcTool.Name);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //congty ct = new congty();
            //ct.Show();
        }

        private void subMnLuat_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            Form3 frm3 = new Form3();
            frm3.Show();
        }
    }
}
